/*
 *@本js功能：登录页
 *@本js作者：杨锡君
 *@编写时间：2017年10月25日
 */
$(function() {
    var Alert1 = new Alert();
    Alert1.init();
    var loading1 = new Loading();
    loading1.init();
    var $login_btn = $(".login_btn");
    var $captchaImage = $("#captchaImage");
    var $loginName = $(".loginName");
    var $loginPass = $(".loginPass");
    var $code = $(".code");
    var login = {
        init: function() {
            this.bindEvent();
        },
        bindEvent: function() {
            var self = this;
            //获取焦点事件
            self.focusEvent($loginName);
            self.focusEvent($loginPass);
            self.focusEvent($code);
            // 更换验证码
            $captchaImage.off("click").click(function() {
                $captchaImage.attr("src", "captcha?timestamp=" + (new Date()).valueOf());
            });
            //			登录按钮
            $login_btn.click(function() {
                var flag = self.loginCheck();
                if(flag) {
                    self.loginEvent();
                }
            });
            //回车登录
            setInterval(function() {
                if($(".alert").attr("style") == "display: none;") {
                    document.onkeydown = function(e) {
                        var ev = document.all ? window.event : e;
                        if(ev.keyCode == 13) {
                            //登录验证
                            var flag = self.loginCheck();
                            if(flag) {
                                //登录
                                self.loginEvent();
                            }
                        }
                    }
                }
            }, 100);
            // 动画结束事件
            $(".ballRotates").bind("webkitAnimationEnd",function () {
                $(this).addClass("noneAnimate");
            });
        },
        focusEvent:function(obj){
            //获取焦点事件
            obj.focus(function(){
                $(this).parent().addClass("inboxFocus");
            });
            obj.blur(function(){
                $(this).parent().removeClass("inboxFocus");
            });
        },
        loginCheck: function() {
            if($loginName.val() == "") {
                Alert1.content = "请输入用户名";
                Alert1.init();
                Alert1.show();
                return false;
            }

            if($loginPass.val() == "") {
                Alert1.content = "请输入密码";
                Alert1.init();
                Alert1.show();
                return false;
            }

            if($code.val() == "") {
                Alert1.content = "请输入验证码";
                Alert1.init();
                Alert1.show();
                return false;
            }
            return true;
        },
        loginEvent: function() {
            var json = JSON.stringify({
                "stuorteaId": $loginName.val(),
                "password": $loginPass.val(),
                "verficateCode": $code.val(),
                "selecetRoleId":1
            });
            loading1.show();
            $.ajax({
                type: 'post',
                url: 'login',
                data: {
                    'jsonStr': json
                },
                success: function(msg) {
                    msg = JSON.parse(msg);
                    loading1.hide();
                    $captchaImage.attr("src", "captcha?timestamp=" + (new Date()).valueOf());
                    if (msg.result == "success") {
                        // var jsons = JSON.stringify({
                        //     "pageName": "index"
                        // });
                        // window.location.href = "skipPage?jsonStr=" + jsons;
                        window.location.href = "skipMain";
                    } else {
                        Alert1.content = msg.errorMsg;
                        $(".code").val("");
                        Alert1.init();
                        Alert1.show();
                    }
                }
            });
        }
    };
    login.init();
});